﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.DataAccessLayer;
using IQKS.AWP.BusinessObject;
using IQKS.AWP.BusinessAccessLayer;
using System.Globalization;

public partial class UploadInvoice : System.Web.UI.Page
{
    public string DocumentPath = ConfigurationSettings.AppSettings["AssessorInvoiceDoc"];
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
            txtInvoiceDate.Attributes.Add("readonly", "readonly");
            txtInvoiceAmount.Attributes.Add("readonly", "readonly");
            txtInvoiceNo.Attributes.Add("readonly", "readonly");
            txtInvoiceComments.Attributes.Add("readonly", "readonly");
            mdlStartDate.Enabled = false;
            //Session["UserID"] = Guid.NewGuid();
        }
    }
    private DataSet GetDataSet()
    {
        DataSet objDs = null;
        try
        {
            string InvoiceID = HttpUtility.UrlDecode(Request.QueryString["InvoiceID"]);
            //objDs = new AssessorInvoiceBAL().GetAssessorInvoice();
            objDs = new AssessorInvoiceBAL().GetInvoiceDetailByInvoiceID(InvoiceID);
            txtInvoiceNo.Text = Convert.ToString(objDs.Tables[0].Rows[0]["InvoiceNo"]);
            txtInvoiceDate.Text = Convert.ToString(objDs.Tables[0].Rows[0]["InvoiceDate"]);
            txtInvoiceAmount.Text = Convert.ToString(objDs.Tables[0].Rows[0]["InvoiceAmount"]);
            txtInvoiceComments.Text = Convert.ToString(objDs.Tables[0].Rows[0]["InvoiceComment"]);
            
            LalblUploadInvoice.Text = "* Acceptable file formats " + Convert.ToString(ConfigurationManager.AppSettings["ReviewFileType"]).Replace("|", ",") + Convert.ToString(ConfigurationManager.AppSettings["FileSize"]);
            if (objDs != null)
            {
                if (objDs.Tables[0].Rows.Count > 0)
                {
                    if (objDs.Tables[0].Rows.Count < 7)
                    {
                        // dvGrid.Style.Add("height", "240px");
                    }
                    else
                    {
                        // dvGrid.Style.Add("height", "320px");
                    }
                }
            }
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
       
        return objDs;
    }
    private void ClearData()
    {
        txtInvoiceNo.Text = string.Empty;
        txtInvoiceDate.Text = string.Empty;
        txtInvoiceAmount.Text = string.Empty;
        txtInvoiceComments.Text = string.Empty;           
        lblMessage.Text = string.Empty;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
         if (Page.IsValid)
         {
             AssessorInvoiceBAL objAIMBal = new AssessorInvoiceBAL();
             AssessorInvoiceBO objAIMBO= new AssessorInvoiceBO();
             //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
             string strReturnValue;

             if (txtUploadInvoice.HasFile)
             {
                
                 InvoiceFileSave();
             }

             objAIMBO.SetMethod = "INSERT";
             objAIMBO.InvoiceID = Convert.ToInt32(HttpUtility.UrlDecode(Request.QueryString["InvoiceID"]));
             objAIMBO.InvoicePath =Path.GetFileName(hdnUploadDoc.Value) ;
             strReturnValue = (string)objAIMBal.SetUploadAssessorInvoice(objAIMBO);

             if (strReturnValue == "SUCCESS")
             {
                 GetData();
                 ClearData();
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invoice saved succesfully.');window.location='../Assessor/AssessorInvoiceDetail.aspx';", true);
                 //lblMessage.Text = "Assessor Invoice saved succesfully.";
                 //lblMessage.ForeColor = System.Drawing.Color.Green;
                 //lblMessage.Font.Bold = true;

             }
             else if (strReturnValue == "EXIST")
             {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Brief already exists.');", true);
                 //lblMessage.Text = "Brief already exists."; //EnrollResources.GetMessage(Message.InsertMessage); // 
                 //lblMessage.ForeColor = System.Drawing.Color.Red;
             }
             else
             {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Error while saving record.');", true);
                 //lblMessage.Text = "Error while saving record."; //EnrollResources.GetMessage(Message.FailedMessage); // 
                 //lblMessage.ForeColor = System.Drawing.Color.Red;
             }
         }

    }
    //protected void gvAInvoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    gvAInvoice.PageIndex = e.NewPageIndex;
    //    GetData();

    //}
    protected void gvAInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvAInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void gvAInvoice_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void GetData()
    {

        gvAInvoice.DataSource = GetDataSet();
        gvAInvoice.DataBind();
    }
    protected void btnCancel_OnClick(object sender, EventArgs e)
    {
       // ClearData();
        Response.Redirect("AssessorInvoiceDetail.aspx");
    }
    private void InvoiceFileSave()
    {
        
        string strPath = string.Empty;
        string strFileExt = string.Empty;
        string strFileName = string.Empty;
        string strFileExt1 = string.Empty;
        strFileExt = Path.GetExtension(txtUploadInvoice.PostedFile.FileName);
        string strDateYY = System.DateTime.Now.ToShortDateString();
        string strDateMM = CommonUtility.ConvertDateToddMMYYY_hhmmss(System.DateTime.Now.ToString());
        string strYYYY = strDateYY.Replace("/", "_");
        string strYYYYM = strDateMM.Remove(0, 11);
        string strYYYYMM = strYYYYM.Replace(":", "_");
        string strDateConYYMM = strYYYY + strYYYYMM;
        strFileName = txtUploadInvoice.FileName.Replace(Convert.ToString(txtUploadInvoice.FileName),(HttpUtility.UrlDecode(Request.QueryString["InvoiceID"])));
        strPath = MapPath(Convert.ToString(ConfigurationManager.AppSettings["AssessorInvoiceDoc"]));
        hdnUploadDoc.Value = strPath + strFileName + "_" + strDateConYYMM + strFileExt;
        //txtUploadInvoice.SaveAs(hdnUploadDoc.Value);
        CommonUtility.SaveFile(txtUploadInvoice, strPath, strFileName + "_" + strDateConYYMM + strFileExt);
    }
}